package com.liu.wangyiapi;

import com.alibaba.fastjson.JSONObject;
import com.liu.entity.ChinaTotal;
import com.liu.service.ChinaTotalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.SimpleTimeZone;

@Component
public class ChinaTotalScheduleTask {
    @Autowired
    private ChinaTotalService chinaTotalService;

    /**
     * 每小时执行一次
     * @throws Exception
     */
    @Scheduled(fixedDelay = 10000) //10s执行一次
    public void updateChinaTotalToDB() throws Exception{
        HttpUtils httpUtils = new HttpUtils();
        String string = httpUtils.getData();

        //1、所有数据的alibaba格式
        JSONObject jsonObject = JSONObject.parseObject(string);
        Object data = jsonObject.get("data");

        //2、data
        JSONObject jsonObjectData = jsonObject.parseObject(data.toString());
        Object chinaTotal = jsonObjectData.get("chinaTotal");
        Object lastUpdateTime = jsonObjectData.get("overseaLastUpdateTime");

        //3、total 全中国整体疫情数据
        JSONObject jsonObjectTotal = JSONObject.parseObject(chinaTotal.toString());
        Object total = jsonObjectTotal.get("total");

        //4、全国数据total
        JSONObject totalData = JSONObject.parseObject(total.toString());
        Object confirm = totalData.get("confirm");
        Object input = totalData.get("input");
        Object severe = totalData.get("severe");
        Object heal = totalData.get("heal");
        Object dead = totalData.get("dead");
        Object suspect = totalData.get("suspect");

        ChinaTotal dataEntity = new ChinaTotal();
        dataEntity.setConfirm((Integer) confirm);
        dataEntity.setInput((Integer) input);
        dataEntity.setSevere((Integer) severe);
        dataEntity.setHeal((Integer) heal);
        dataEntity.setDead((Integer) dead);
        dataEntity.setSuspect((Integer) suspect);

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        dataEntity.setUpdateTime(format.parse(String.valueOf(lastUpdateTime)));

        //6、插入数据库
        chinaTotalService.save(dataEntity);
    }
}
